package com.service;

import com.dao.CartDao;
import com.entity.Carts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/*
* 购物车
* */
@Service
public class CartService {
    @Autowired
    private CartDao cartDao;
    @Autowired
    private GoodService goodService;

    public int getCount(int userId){
        return cartDao.selectSumAmountByUserId(userId);
    }

    public boolean save(int goodId,int userId){
        Carts cart=cartDao.selectByUserIdAndGoodId(userId,goodId);
        if (Objects.nonNull(cart)){
            return cartDao.updateAmount(cart.getId(),1);
        }
        cart=new Carts();
        cart.setGoodId(goodId);
        cart.setUserId(userId);
        cart.setAmount(1);
        return cartDao.insert(cart);
    }
}
